package com.chenjt.netty.eight_nio.bufferAttribute;

import java.nio.ByteBuffer;

/**
 * 注释：Slice Buffer与原有buffer共享相同的底层数组
 * 修改slice出来的buffer，源buffer也会跟着修改，反之也一样
 *
 * @author chenjt
 * @date 2024/8/21 14:12
 */
public class BufferSlice {

    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(10);

        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte)i);
        }
        buffer.position(2);
        buffer.limit(6);

        ByteBuffer sliceBuffer = buffer.slice();

        for (int i = 0; i < sliceBuffer.capacity(); i++) {
            byte b = sliceBuffer.get(i);
            b *= 2;
            sliceBuffer.put(i,b);
        }

        buffer.position(0);
        buffer.limit(buffer.capacity());

        while (buffer.hasRemaining()){
            System.out.println(buffer.get());
        }



    }

}
